﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WP09S03_OfficeTracking.Controller;

namespace WP09S03_OfficeTracking.UserControls
{
    public delegate void DelSelectedEmployeeEvent(string id, string name);

    public delegate void DelResultEvent(int numRecords);

    public partial class EmployeeUControl : UserControl
    {
        public event DelSelectedEmployeeEvent OnSelectedEmployeeEvent;
        public event DelResultEvent OnSearchResultEvent;

        private WP09S03_OfficeTracking.Controller.EmployeController controller;

        public EmployeeUControl()
        {
            InitializeComponent();
            controller = new EmployeController();
        }

        public void RefreshData()
        {
            controller.GetEmployeesByIDName(this.dataGridViewEmployees,
                        textBoxID.UserText, textBoxName.UserText);
            this.dataGridViewEmployees.Columns["Valid"].Visible = false;

        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            int cnt = controller.GetEmployeesByIDName(this.dataGridViewEmployees,
                textBoxID.UserText, textBoxName.UserText);
            this.dataGridViewEmployees.Columns["Valid"].Visible = false;
            if (OnSearchResultEvent != null)
                OnSearchResultEvent(cnt);
            
        }

        private void dataGridViewEmployees_Click(object sender, EventArgs e)
        {
            if (dataGridViewEmployees.SelectedRows.Count > 0)
            {
                if (OnSelectedEmployeeEvent != null)
                {
                    string id = dataGridViewEmployees.SelectedRows[0].Cells[0].Value.ToString();
                    string name = dataGridViewEmployees.SelectedRows[0].Cells[1].Value.ToString();
                    id = id.Trim();
                    name = name.Trim();                 
                    OnSelectedEmployeeEvent(id, name);
                }
            }
        }
    }
}
